package com.link.base.base.contactinfo.controller;

import com.link.base.base.contactinfo.model.ContactInfo;
import com.link.base.base.contactinfo.service.ContactInfoService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.DateUtil;
import com.link.core.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 联系人
 *
 * @author 王昊
 * @date 2020-09-24 10:56:56
 */
@Controller
@RequestMapping("/link/contactInfo")
public class ContactInfoController extends BasicController<ContactInfo> {

    private static final Logger logger = LogManager.getLogger(ContactInfoController.class);

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private ContactInfoService contactInfoService;

    @Override
    public BasicService<ContactInfo> getBasicService() throws Exception {
        return contactInfoService;
    }

    /**
     * 更新状态
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping({"/changeStatus"})
    @ResponseBody
    public Map<String, Object> changeStatus(@JsonParam ContactInfo entity,
                                            HttpSession session,
                                            HttpServletRequest request,
                                            HttpServletResponse response) {
        Map<String, Object> result = new HashMap(4);

        try {
            ContactInfo newEntry = this.getBasicService().queryById(entity);

            if (Objects.isNull(newEntry)) {
                throw new BasicControllerException("记录不存在，请检查！");
            }
            if (StringUtils.equals(newEntry.getStatus(), "Inactive")) {
                throw new BasicControllerException("该记录已失效，不允许重复操作！");
            }

            if (StringUtils.equals(entity.getStatus(), "Inactive")) {
                entity.setUactTime(DateUtil.dateToStr(new Date(), DateUtil.DATE_TIME));
            } else {
                throw new BasicControllerException("设置失效状态参数异常，请检查！");
            }
            entity.setUpdateFields("uactTime,status");
            this.getBasicService().update(entity);
            newEntry = this.getBasicService().queryById(entity);
            result.put("newRow", newEntry);
            result.put("success", true);
            result.put("code", "200");
        } catch (Exception var8) {
            logger.error("BasicController捕获异常: ", var8);
            result.put("success", false);
            result.put("result", var8.getMessage());
            result.put("code", "400");
        }

        return result;
    }

}
